package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.transportline.TransportTripsDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.entity.transportline.PdTransportTrips;
import com.itheima.pinda.mapper.PdTransportTripsMapper;
import com.itheima.pinda.mapstruct.TransportDTO2Entity;
import com.itheima.pinda.service.PdTransportTripsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PdTransportTripsServiceImpl extends ServiceImpl<PdTransportTripsMapper, PdTransportTrips> implements PdTransportTripsService {

    @Resource
    private TransportDTO2Entity dto2Entity;

    /**
     * 添加车次
     *
     * @param dto 车次信息
     * @return 车次信息
     */
    @Override
    public TransportTripsDto saveTransportTrips(TransportTripsDto dto) {
        PdTransportTrips pdTransportTrips = dto2Entity.conver(dto);
        super.save(pdTransportTrips);
        dto = dto2Entity.conver(pdTransportTrips);
        return dto;
    }

    /**
     * 获取车次列表
     *
     * @param transportLineId 线路类型id
     * @param ids             车次id列表
     * @return 车次列表
     */
    @Override
    public List<TransportTripsDto> findAll(String transportLineId, List<String> ids) {
        List<PdTransportTrips> pdTransportTripsList = super.list(Wrappers.<PdTransportTrips>lambdaQuery()
                .eq(StringUtils.isNotBlank(transportLineId), PdTransportTrips::getTransportLineId, transportLineId)
                .in(ids != null && ids.size() > 0, PdTransportTrips::getId, ids)
                .eq(PdTransportTrips::getStatus, Constant.DATA_DEFAULT_STATUS)
                .orderByAsc(PdTransportTrips::getId));

        return pdTransportTripsList.stream()
                .map(item -> dto2Entity.conver(item))
                .collect(Collectors.toList());
    }
}
